package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskServiceBundlesMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskServiceBundles;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskServiceCallTasks;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskServiceBundlesService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskServiceBundlesServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskServiceBundlesMapper, TaskAutotaskServiceBundles>
        implements TaskAutotaskServiceBundlesService {

    private final TaskAutotaskServiceBundlesMapper taskAutotaskServiceBundlesMapper;

    @Override
    public void sync(ResultEntity content) {


        log.debug("Autotask serviceBundles receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskServiceBundles data = TaskAutotaskServiceBundles.builder()
                .serviceBundlesId(super.getLongValue(entity.get("id")))
                .billingCodeId(super.getIntegerValue(entity.get("billingCodeID")))
                .createDate(super.getDateTimeValue(entity.get("createDate")))
                .creatorResourceId(super.getIntegerValue(entity.get("creatorResourceID")))
                .description(super.getStringValue(entity.get("description")))
                .invoiceDescription(super.getStringValue(entity.get("invoiceDescription")))
                .isActive(super.getBooleanValue(entity.get("isActive")))
                .lastModifiedDate(super.getDateTimeValue(entity.get("lastModifiedDate")))
                .serviceBundlesName(super.getStringValue(entity.get("name")))
                .percentageDiscount(super.getDoubleValue(entity.get("percentageDiscount")))
                .periodType(super.getIntegerValue(entity.get("periodType")))
                .serviceLevelAgreementId(super.getLongValue(entity.get("serviceLevelAgreementID")))
                .unitCost(super.getDoubleValue(entity.get("unitCost")))
                .unitDiscount(super.getDoubleValue(entity.get("unitDiscount")))
                .unitPrice(super.getDoubleValue(entity.get("unitPrice")))
                .updateResourceId(super.getIntegerValue(entity.get("updateResourceID")))
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());


        TaskAutotaskServiceBundles dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskServiceBundles>lambdaQuery()
                .eq(TaskAutotaskServiceBundles::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskServiceBundles::getServiceBundlesId, data.getServiceBundlesId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }


    }
}
